package com.jimikongjian.service.dao.impl;

import com.jimikongjian.dao.IUser;
import com.jimikongjian.models.User;
import com.jimikongjian.service.IP;
import com.jimikongjian.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by baidu on 17/2/12.
 * @author fuxiuyang
 * 继承IUser接口,实现IUser的接口,并实现数据的检测
 */
@Service("iUserImpl")
public class IUserImpl implements IUser {

//   默认为md5加密,长度为32位
    private int passwordLength = 32;

    @Autowired
//    @Qualifier("userMapper")
    private IUser userMapper;

    @Autowired
    @Qualifier("mailService")
    private MailService mailService;

    @Autowired
    @Qualifier("ip")
    private IP ip;

    /**
     * 通过用户的id号，查询数据库中相应的用户
     *
     * @param id
     * @return
     */
    public List<User> getUserListById(int id) {
        if(id > 0){
            return userMapper.getUserListById(id);
        }
        return null;
    }

    /**
     * 通过邮箱地址查询用户信息
     * @param emailAddress
     * @return List<User> 如果邮箱地址不合法,则不进行查询,直接返回null
     */
    public List<User> getUserListByEmail(String emailAddress) {
        if(mailService.isEmail(emailAddress)){
            return userMapper.getUserListByEmail(emailAddress);
        }
        return null;
    }

    /**
     * 通过邮箱查询单一用户信息
     * @param emailAddress
     * @return User
     */
    public User getUserByEmail(String emailAddress){
        List<User> users = getUserListByEmail(emailAddress);
        if(users != null && users.size() > 0){
            return users.get(0);
        }
        return null;
    }

    /**
     * 通过邮箱地址获取响应用户的id号
     * @param emailAddress
     * @return 如果存在该用户,咱返回该用户的id号,如果不存在则返回-1
     */
    public int getUserIdByEmail(String emailAddress){
        List<User> users = getUserListByEmail(emailAddress);
        if(users != null && users.size() > 0){
            return users.get(0).getId();
        }
        return -1;
    }
    /**
     * 插入用户信息
     * @param user
     * @return 如果用户的邮箱密码不合法或者是邮箱和密码为空,则直接返回false
     */
    public boolean insertUser(User user) {
        if(user.getPassword().length() == passwordLength && mailService.isEmail(user.getEmail())){
            userMapper.insertUser(user);
            return true;
        }
        return false;
    }

    /**
     * 插入用户信息
     * @param emailAdress
     * @param password
     * @return 插入成功则返回ture,否则返回false
     */
    public boolean insertUser(String emailAdress,String password){
        if(mailService.isEmail(emailAdress) && password.length() == passwordLength){
            User user = new User();
            user.setEmail(emailAdress);
            user.setPassword(password);
            insertUser(user);
            return true;
        }
        return false;
    }

    /**
     * 更新用户密码,
     * 用户对象属性必填项:mailService,password
     * @param user
     * @return 如果用户的邮箱密码不合法,则直接返回false
     */

    public boolean updateUserPassword(User user) {
        if(user.getPassword().length() == passwordLength && mailService.isEmail(user.getEmail())){
            userMapper.updateUserPassword(user);
            return true;
        }
        return false;
    }

    /**
     * 通过emailAddress更新用户的密码password
     * @param emailAdress
     * @param password
     * @return 更新成功返回ture,否则会false
     */
    public boolean updateUserPassword(String emailAdress,String password){
            User user = getUserByEmail(emailAdress);
            if(user != null){
                user.setPassword(password);
                return updateUserPassword(user);
            }
            return false;
    }
    /**
     * 更新用户状态,
     * 用户对象属性必填项:mailService,status
     * @param user
     * @return 如果更新成功则返回true,否则返回false
     */
    public boolean updateUserStatus(User user) {
        if(mailService.isEmail(user.getEmail())){
            userMapper.updateUserStatus(user);
            return true;
        }
        return false;
    }

    /**
     * 更改邮箱地址为emailAddress用户的用户状态为status
     * @param emailAddress
     * @param status
     * @return 如果更新成功则返回true,否则返回false
     */
    public boolean updateUserStatus(String emailAddress, int status){
        if(status == 0 || status == 1){
            User user = getUserByEmail(emailAddress);
            if(user != null){
                user.setStatus(status);
                return updateUserStatus(user);
            }
        }
        return false;
    }

    /**
     * 更新帐号状态,
     * 用户对象属性必填项:mailService,type
     * @param user
     * @return 如果更新成功则返回true,否则返回false
     */
    public boolean updateUserType(User user) {
        if(mailService.isEmail(user.getEmail())){
            userMapper.updateUserType(user);
            return true;
        }
        return false;
    }

    /**
     * 更改邮箱地址为emailAddress用户的用户状态为type
     * @param emailAddress
     * @param type
     * @return 如果更新成功则返回true,否则返回false
     */
    public boolean updateUserType(String emailAddress,int type){
        if(type == 0 || type == 1 || type == 2){
            User user = getUserByEmail(emailAddress);
            if(user != null){
                user.setType(type);
                return updateUserType(user);
            }
        }
        return false;
    }

    /**
     * 更新用户最后登录的信息
     * 用户对象属性必填项:mailService,lastLoginTime,lastLoginIp
     * @param user
     * @return 如果用户的邮箱密码不合法或者是最后登录时间和最后登录ip为空,则直接返回false
     */
    public boolean updateUserLastLoginMessage(User user) {
        if(mailService.isEmail(user.getEmail()) && user.getLastLoginTime() != null && user.getLastLoginIp() != null){
            userMapper.updateUserLastLoginMessage(user);
            return true;
        }
        return false;
    }

    /**
     * 更新用户
     *
     * @param user
     * @return 更新成功返回true, 否则返回false
     */
    public boolean updateUser(User user) {
        if(mailService.isEmail(user.getEmail())){
            userMapper.updateUser(user);
            return true;
        }
        return false;
    }

    /**
     * 更新邮箱为emailAddress的用户的最后登录时间lastLoginTime和最后登录Ip
     * @param emailAddress
     * @param lastLoginTime
     * @param lastLoginIp
     * @return 如果更新成功则返回true,否则返回false
     */
    public boolean updateUserLastLoginMessage(String emailAddress, Date lastLoginTime, String lastLoginIp){
        if(ip.isIP(lastLoginIp)){
            User user = getUserByEmail(emailAddress);
            if(user != null){
                user.setLastLoginTime(lastLoginTime);
                user.setLastLoginIp(lastLoginIp);
                return updateUserLastLoginMessage(user);
            }
        }
        return false;
    }

    /**
     * 删除用户信息
     * 用户对象属性必填项:mailService
     * @param user
     * @return 如果用户的邮箱密码不合法,则直接返回false
     */
    public boolean deleteUser(User user) {
        if(mailService.isEmail(user.getEmail())){
            userMapper.deleteUser(user);
            return true;
        }
        return false;
    }

    /**
     * 通过用户的邮箱地址删除用户
     * @param emailAddress
     * @return 删除成功则返回true,否则返回false
     */
    public boolean deleteUser(String emailAddress){
        User user = new User();
        user.setEmail(emailAddress);
        return deleteUser(user);
    }
}
